package com.example.weiting.huishenghuo.com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.weiting.huishenghuo.R;
import com.example.weiting.huishenghuo.utils.Message;
import com.example.weiting.huishenghuo.utils.MessageAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class Record extends AppCompatActivity {
    List<Message> messageList = new ArrayList<Message>();
    public final static int REQUEST_CODE = 1;
    TextView tv1,tv2;
    int spend;
    int zhuan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        tv1 = (TextView) findViewById(R.id.textview);
        tv2 = (TextView) findViewById(R.id.textview1);
        //测试数据
        Message message = new Message("测试数据", -500);
        message.setDate(new Date(2020-1900,7-1,5,8,30,0));
        messageList.add(message);
        Message message1 = new Message("测试数据", 500);
        message1.setDate(new Date(2020-1900,6-1,5,8,30,0));
        messageList.add(message1);
        Message message2 = new Message("测试数据", -1000);
        messageList.add(message2);
        Message message3 = new Message("测试数据", 1000);
        messageList.add(message3);
        //设置上面的值
        spend = 0;
        zhuan = 0;
        for (int i = 0; i < messageList.size(); i++) {
            if (messageList.get(i).getZhuangTai() > 0) {
                spend += messageList.get(i).getZhuangTai();
            } else {
                zhuan += messageList.get(i).getZhuangTai();
            }
        }
        tv1.setText(String.valueOf(spend));
        tv2.setText(String.valueOf(zhuan));
        //显示ViewList中的数据。
        MessageAdapter adapter = new MessageAdapter(Record.this, R.layout.list_item, messageList);
        ListView messagesView = (ListView) findViewById(R.id.cmc);
        messagesView.setAdapter(adapter);

        //为ViewList添加监听事件
        messagesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            //int i 代表是第i个元素
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //将id和List
                Intent intent = new Intent(Record.this,ModifyDelete.class);
                Bundle args = new Bundle();
                args.putSerializable("id", i);
                args.putSerializable("messageList", (Serializable) messageList);
                intent.putExtra("messages", args);
                intent.putExtra("id",args);
                startActivityForResult(intent,REQUEST_CODE);
            }
        });

        //获取增添按钮
        Button button = (Button) findViewById(R.id.button2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //将messageList集合发送到处理界面，期待返回一个新的messageList。
                Intent intent = new Intent(Record.this, Add.class);
                Bundle args = new Bundle();
                args.putSerializable("messageList", (Serializable) messageList);
                intent.putExtra("messages", args);
                //期待有一个返回值。
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

        //明细按钮功能实现
        Button button1 = (Button) findViewById(R.id.button3);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //将messageList集合发送到明细界面
                Intent intent = new Intent(Record.this, Select.class);
                Bundle args = new Bundle();
                args.putSerializable("messageList", (Serializable) messageList);
                intent.putExtra("messages", args);
                startActivity(intent);
            }
        });
    }
    //重写返回函数，得到返回的参数后要做的事情在onActivityResult中写。
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        //if用来判断返回是哪个界面的返回。REQUEST_CODE和Add.RESULT_CODE都是自己定义的值。
        //第一个if用来判断返回的值是不是从本页面传出的
        //第二个if用来判断返回的值来自哪个页面。这里写到一块是因为要做的事情一样。ModifyDelete.RESULT_CODE=2
        if (requestCode == REQUEST_CODE) {
            if (resultCode == Add.RESULT_CODE || resultCode == ModifyDelete.RESULT_CODE) {

                //获取返回的messageList集合。
                Bundle args = intent.getBundleExtra("messages");
                messageList = (ArrayList<Message>) args.getSerializable("messageList");

                //重新显示ListView中的数据。
                MessageAdapter adapter = new MessageAdapter(Record.this, R.layout.list_item, messageList);
                ListView messagesView = (ListView) findViewById(R.id.cmc);
                messagesView.setAdapter(adapter);

                //设置最上面的值
                spend = 0;
                zhuan = 0;
                for (int i = 0; i < messageList.size(); i++) {
                    if (messageList.get(i).getZhuangTai() > 0) {
                        spend += messageList.get(i).getZhuangTai();
                    } else {
                        zhuan += messageList.get(i).getZhuangTai();
                    }
                }
                tv1.setText(String.valueOf(spend));
                tv2.setText(String.valueOf(zhuan));
            }
        }
    }
}
